package com.koron.css2.serviceManage.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.CompanyAreaBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.CompanyAreaMapper;
import com.koron.css2.serviceManage.utils.CommonUtils;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bson.types.ObjectId;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 片区-添加
 * 
 * @author 李攀
 *
 */
public class CompanyAreaAdd implements ServerInterface {

	private static Logger logger = LogManager.getLogger(CompanyAreaAdd.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			CompanyAreaMapper mapper = factory.getMapper(CompanyAreaMapper.class);
			CompanyAreaBean bean = JsonUtils.objectToPojo(req.getData(), CompanyAreaBean.class);
			// 校验字段重复
			if (mapper.check("area_no", bean.getAreaNo()) > 0) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "接口编号：" + bean.getAreaNo() + "的信息已存在。",
						void.class);
			}
			if (mapper.check("area_name", bean.getAreaName()) > 0) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "接口名称：" + bean.getAreaName() + "的信息已存在。",
						void.class);
			}
			bean.setCompanyAreaId(new ObjectId().toHexString());
			bean.setCreateName(userInfo.getUserInfo().getName());
			bean.setCreateTime(CommonUtils.getCurrentTime());
			mapper.insertCompanyArea(bean);
		} catch (Exception e) {
			logger.error("非法参数", e);
			return MessageBean.create(Constant.ILLEGAL_PARAMETER, "非法参数", void.class);
		}
		return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
	}

}
